﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls.HYListView;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public partial class FrmSystemShowHide : System.Windows.Forms.Form
	{
		public FrmSystemShowHide()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
		}

		public FrmSystemShowHide(UIDocument uiDocument, string sysName)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			//base.Icon = Resource.YArchitechIcon;
			this.uiDoc = uiDocument;
			this.doc = this.uiDoc.Document;
			this.commandSysName = sysName;
			this.projectName = this.doc.Title;
			this.mepSystems = this.GetMepSystemsBySysName(this.commandSysName);
			int num = 100;
			if (this.mepSystems != null && this.mepSystems.Count > 0)
			{
				foreach (MEPSystem mepsystem in this.mepSystems)
				{
					int num2 = StringTools.GetStringByteLength(mepsystem.Name) * 8;
					num = ((num < num2) ? num2 : num);
				}
			}
			this.hyListView1.Columns.Add(new ColumnHeader
			{
				Text = "定位",
				Width = 38,
				Name = "ColLocation"
			});
			this.hyListView1.Columns.Add(new ColumnHeader
			{
				Text = "隐显",
				Width = 38,
				Name = "ColShow"
			});
			this.hyListView1.Columns.Add(new ColumnHeader
			{
				Text = "选择",
				Width = 38,
				Name = "ColSelect"
			});
			this.hyListView1.Columns.Add(new ColumnHeader
			{
				Text = "系统名称",
				Width = num,
				Name = "ColSysName"
			});
			ImageList imageList = new ImageList();
			imageList.ImageSize = new Size(1, 30);
			this.hyListView1.SmallImageList = imageList;
			base.Width = num + 114 + 50;
		}

		private void FrmSystemShowHide_Load(object sender, EventArgs e)
		{
			this.hyListView1.Items.Clear();
			this.DrawButtonAndLabel(this.commandSysName, this.mepSystems);
			this.SelectButton(this.tatelButton);
		}

		public int DrawButtonAndLabel(string sortSystemName, List<MEPSystem> systemList)
		{
			int result = 0;
			if (sortSystemName == "暖通" && systemList != null)
			{
				foreach (MEPSystem mepSystem in systemList)
				{
					this.DrawControls(mepSystem, ref result);
				}
			}
			if (sortSystemName == "消防排水" && systemList != null)
			{
				foreach (MEPSystem mepSystem2 in systemList)
				{
					this.DrawControls(mepSystem2, ref result);
				}
			}
			if (sortSystemName == "电气" && systemList != null)
			{
				foreach (MEPSystem mepSystem3 in systemList)
				{
					this.DrawControls(mepSystem3, ref result);
				}
			}
			return result;
		}

		private void DrawControls(MEPSystem mepSystem, ref int labelWidth)
		{
			int num = StringTools.GetStringByteLength(mepSystem.Name) * 8;
			if (labelWidth < num)
			{
				labelWidth = num;
			}
			ListViewItem listViewItem = new ListViewItem();
			this.hyListView1.Items.Add(listViewItem);
			int index = listViewItem.Index;
			Element element = null;
			try
			{
				if (mepSystem.Elements == null || mepSystem.Elements.Size <= 0)
				{
					return;
				}
				ElementSetIterator elementSetIterator = mepSystem.Elements.ForwardIterator();
				elementSetIterator.Reset();
				if (elementSetIterator.MoveNext())
				{
					element = (elementSetIterator.Current as Element);
				}
			}
			catch
			{
				element = this.doc.GetElement(mepSystem.Id);
			}
			HYListViewButtonCell hylistViewButtonCell = new HYListViewButtonCell();
			hylistViewButtonCell.Btn.Tag = mepSystem;
			hylistViewButtonCell.Btn.Image = this.LoadBtnImage(this.locateIamge);
			hylistViewButtonCell.Btn.AccessibleDescription = "location";
			hylistViewButtonCell.Btn.Click += this.ButtonLocation;
			listViewItem.SubItems.Add(hylistViewButtonCell);
			this.hyListView1.AddControlToSubItem(0, index, hylistViewButtonCell, DockStyle.Fill);
			HYListViewButtonCell hylistViewButtonCell2 = new HYListViewButtonCell();
			hylistViewButtonCell2.Btn.Tag = mepSystem;
			hylistViewButtonCell2.Btn.AccessibleDescription = "show";
			hylistViewButtonCell2.Btn.Click += this.ButtonShowHide;
			listViewItem.SubItems.Add(hylistViewButtonCell2);
			this.hyListView1.AddControlToSubItem(1, index, hylistViewButtonCell2, DockStyle.Fill);
			if (element.IsHidden(this.doc.ActiveView))
			{
				hylistViewButtonCell2.Btn.Image = this.LoadBtnImage(this.hideImage);
				hylistViewButtonCell2.Btn.AccessibleName = "false";
			}
			else
			{
				hylistViewButtonCell2.Btn.Image = this.LoadBtnImage(this.showImage);
				hylistViewButtonCell2.Btn.AccessibleName = "true";
			}
			HYListViewButtonCell hylistViewButtonCell3 = new HYListViewButtonCell();
			hylistViewButtonCell3.Btn.Tag = mepSystem;
			hylistViewButtonCell3.Btn.Image = this.LoadBtnImage(this.brushImage);
			hylistViewButtonCell3.Btn.AccessibleDescription = "brush";
			hylistViewButtonCell3.Btn.Click += this.ButtonBrush;
			listViewItem.SubItems.Add(hylistViewButtonCell3);
			this.hyListView1.AddControlToSubItem(2, index, hylistViewButtonCell3, DockStyle.Fill);
			HYListViewButtonCell hylistViewButtonCell4 = new HYListViewButtonCell();
			hylistViewButtonCell4.Btn.Tag = mepSystem;
			hylistViewButtonCell4.Btn.Text = mepSystem.Name;
			hylistViewButtonCell4.Btn.Enabled = false;
			hylistViewButtonCell4.Btn.AccessibleDescription = "text";
			hylistViewButtonCell4.Btn.FlatStyle = FlatStyle.Flat;
			hylistViewButtonCell4.Btn.TextAlign = ContentAlignment.MiddleLeft;
			listViewItem.SubItems.Add(hylistViewButtonCell4);
			this.hyListView1.AddControlToSubItem(3, index, hylistViewButtonCell4, DockStyle.Fill);
		}

		private Image LoadBtnImage(string imgFullName)
		{
			Image result = null;
			if (File.Exists(imgFullName))
			{
				result = Image.FromFile(imgFullName);
			}
			return result;
		}

		private void ButtonBrush(object sender, EventArgs e)
		{
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			this.tatelButton = button;
			this.SelectButton(button);
			this.specialSysName = (button.Tag as MEPSystem).Name;
			base.DialogResult = DialogResult.Retry;
		}

		private void SelectButton(Button button)
		{
			if (button == null)
			{
				return;
			}
			MEPSystem mepsystem = button.Tag as MEPSystem;
			if (mepsystem == null)
			{
				return;
			}
			System.Drawing.Color transparent = System.Drawing.Color.Transparent;
			foreach (object obj in this.hyListView1.Controls)
			{
				Button button2 = ((System.Windows.Forms.Control)obj) as Button;
				if (button2 != null)
				{
					MEPSystem mepsystem2 = button2.Tag as MEPSystem;
					if (button2.AccessibleDescription == "text")
					{
						if (mepsystem2 == mepsystem)
						{
							button2.BackColor = System.Drawing.Color.LightSkyBlue;
						}
						else
						{
							button2.BackColor = transparent;
						}
					}
				}
			}
		}

		private void ButtonLocation(object sender, EventArgs e)
		{
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			this.tatelButton = button;
			this.SelectButton(button);
			this.specialSysName = (button.Tag as MEPSystem).Name;
			ElementSet elementSet = this.doc.Application.Create.NewElementSet();
			List<string> elemSetId = new List<string>();
			elemSetId = this.GetElementsBySysName(this.specialSysName);
			elementSet = this.GetElemSetByParamValue(this.specialSysName, elemSetId);
			Transaction transaction = new Transaction(this.doc, "系统定位");
			transaction.Start();
			RevitVersionFuncs.ClearSelection(this.uiDoc.Selection);
			List<Element> list = new List<Element>();
			foreach (object obj in elementSet)
			{
				Element item = (Element)obj;
				list.Add(item);
			}
			RevitVersionFuncs.SetSelection(this.uiDoc.Selection, list);
			transaction.Commit();
			this.uiDoc.ShowElements(elementSet);
		}

		private void ButtonShowHide(object sender, EventArgs e)
		{
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			this.tatelButton = button;
			this.SelectButton(button);
			MEPSystem mepsystem = button.Tag as MEPSystem;
			if (mepsystem == null)
			{
				return;
			}
			ElementSet elementSet = this.doc.Application.Create.NewElementSet();
			Transaction transaction = new Transaction(this.doc, "系统图元隐现");
			transaction.Start();
			if (button.AccessibleName == "false")
			{
				try
				{
					if (mepsystem != null)
					{
						this.doc.ActiveView.UnHideYJKElements(mepsystem.Elements);
					}
				}
				catch
				{
					Element element = this.doc.GetElement(mepsystem.Id);
					ElementSet elementSet2 = new ElementSet();
					elementSet2.Insert(element);
					this.doc.ActiveView.UnHideYJKElements(elementSet2);
				}
				List<string> elemSetId = new List<string>();
				elemSetId = this.GetElementsBySysName(mepsystem.Name);
				elementSet = this.GetElemSetByParamValue(mepsystem.Name, elemSetId);
				if (elementSet != null && elementSet.Size > 0)
				{
					this.doc.ActiveView.UnHideYJKElements(elementSet);
				}
				button.Image = Image.FromFile(this.showImage);
				button.AccessibleName = "true";
			}
			else
			{
				try
				{
					if (mepsystem != null)
					{
						this.doc.ActiveView.HideYJKElements(mepsystem.Elements);
					}
				}
				catch
				{
					Element element2 = this.doc.GetElement(mepsystem.Id);
					ElementSet elementSet3 = new ElementSet();
					elementSet3.Insert(element2);
					this.doc.ActiveView.HideYJKElements(elementSet3);
				}
				elementSet = this.GetElemSetByParamValue(mepsystem.Name, this.GetElementsBySysName(mepsystem.Name));
				if (elementSet != null && elementSet.Size > 0)
				{
					this.doc.ActiveView.HideYJKElements(elementSet);
				}
				button.Image = Image.FromFile(this.hideImage);
				button.AccessibleName = "false";
			}
			transaction.Commit();
		}

		public ElementSet GetElemSetByParamValue(string paramValue, List<string> elemSetId)
		{
			BuiltInParameter parameterId = BuiltInParameter.RBS_SYSTEM_NAME_PARAM;
			HYSystemElementFilter hysystemElementFilter = new HYSystemElementFilter(this.doc);
			ElementSet elementSet = this.doc.Application.Create.NewElementSet();
			FilteredElementIterator elementIterator = hysystemElementFilter.GetFilterByCondition(null, false, this.doc.ActiveView).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element != null)
				{
					if (elemSetId != null && elemSetId.Count > 0)
					{
						using (List<string>.Enumerator enumerator = elemSetId.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								if (enumerator.Current.Equals(element.Id.ToString()))
								{
									elementSet.Insert(element);
								}
							}
						}
					}
					Parameter parameter = element.GetParameter(parameterId);
					if (parameter != null && parameter.HasValue && parameter.AsString().Equals(paramValue))
					{
						elementSet.Insert(element);
					}
				}
			}
			if (elementSet.Size > 0)
			{
				return elementSet;
			}
			return null;
		}

		public List<string> GetElementsBySysName(string sysName)
		{
			List<string> list = new List<string>();
			list = new RecordSystemElements(this.projectName).GetAllRecordInfo(sysName);
			if (list == null || list.Count < 1)
			{
				return null;
			}
			return list;
		}

		public void RecordElementToSystem(string sysName, IList<Element> listElement)
		{
			if (listElement == null || listElement.Count < 1)
			{
				return;
			}
			List<string> list = new List<string>();
			foreach (Element element in listElement)
			{
				list.Add(element.Id.ToString());
			}
			new RecordSystemElements(this.projectName).WriteElementsID(sysName, list);
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			if (keyData == Keys.Escape)
			{
				base.Close();
				return true;
			}
			return base.ProcessCmdKey(ref msg, keyData);
		}

		public List<MEPSystem> GetMepSystemsBySysName(string sortSystemName)
		{
			List<MEPSystem> list = new List<MEPSystem>();
			if (sortSystemName == "暖通")
			{
				List<MechanicalSystem> allDuctSystem = this.GetAllDuctSystem();
				if (allDuctSystem != null)
				{
					list.AddRange(allDuctSystem);
				}
				List<PipingSystem> list2 = this.SortPipingSystem(this.GetAllPipeSystem(), "HVAC");
				if (list2 != null)
				{
					list.AddRange(list2);
				}
				if (list.Count > 0)
				{
					return list;
				}
				return null;
			}
			else if (sortSystemName == "消防排水")
			{
				List<PipingSystem> list3 = this.SortPipingSystem(this.GetAllPipeSystem(), "Water");
				if (list3 != null)
				{
					list.AddRange(list3);
				}
				List<PipingSystem> list4 = this.SortPipingSystem(this.GetAllPipeSystem(), "Fire");
				if (list4 != null)
				{
					list.AddRange(list4);
				}
				if (list.Count > 0)
				{
					return list;
				}
				return null;
			}
			else
			{
				if (!(sortSystemName == "电气"))
				{
					return null;
				}
				List<ElectricalSystem> electricalSystem = this.GetElectricalSystem();
				if (electricalSystem != null)
				{
					list.AddRange(electricalSystem);
				}
				if (list.Count > 0)
				{
					return list;
				}
				return null;
			}
		}

		private List<ElectricalSystem> GetElectricalSystem()
		{
			List<ElectricalSystem> list = new List<ElectricalSystem>();
			FilteredElementIterator elementIterator = new HYSystemElementFilter(this.doc).GetElementsByBICategory(BuiltInCategory.OST_ElectricalCircuit).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element is ElectricalSystem)
				{
					list.Add(element as ElectricalSystem);
				}
			}
			if (list.Count > 0)
			{
				return list;
			}
			return null;
		}

		private List<MechanicalSystem> GetAllDuctSystem()
		{
			List<MechanicalSystem> list = new List<MechanicalSystem>();
			FilteredElementIterator elementIterator = new HYSystemElementFilter(this.doc).GetElementsByBICategory(BuiltInCategory.OST_DuctSystem).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element is MechanicalSystem)
				{
					list.Add(element as MechanicalSystem);
				}
			}
			if (list.Count > 0)
			{
				return list;
			}
			return null;
		}

		private List<PipingSystem> GetAllPipeSystem()
		{
			List<PipingSystem> list = new List<PipingSystem>();
			FilteredElementIterator elementIterator = new HYSystemElementFilter(this.doc).GetElementsByBICategory(BuiltInCategory.OST_PipingSystem).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element is PipingSystem)
				{
					list.Add(element as PipingSystem);
				}
			}
			if (list.Count > 0)
			{
				return list;
			}
			return null;
		}

		private List<PipingSystem> SortPipingSystem(List<PipingSystem> pipesSystem, string typeSys)
		{
			List<PipingSystem> list = new List<PipingSystem>();
			List<PipingSystem> list2 = new List<PipingSystem>();
			List<PipingSystem> list3 = new List<PipingSystem>();
			if (pipesSystem == null || pipesSystem.Count < 1)
			{
				return null;
			}
			foreach (PipingSystem pipingSystem in pipesSystem)
			{
				PipeSystemType systemType = pipingSystem.SystemType;
				if (systemType == 29 || systemType == 7 || systemType == 8 || systemType == 22 || systemType == 17)
				{
					list.Add(pipingSystem);
				}
				if (systemType == 24 || systemType == 26 || systemType == 25 || systemType == 23 || systemType == 28)
				{
					list3.Add(pipingSystem);
				}
				if (systemType == 16 || systemType == 20 || systemType == 19)
				{
					list2.Add(pipingSystem);
				}
			}
			if (list.Count <= 0)
			{
				list = null;
			}
			if (list2.Count <= 0)
			{
				list2 = null;
			}
			if (list3.Count <= 0)
			{
				list3 = null;
			}
			if (typeSys == "HVAC")
			{
				return list;
			}
			if (typeSys == "Water")
			{
				return list2;
			}
			if (typeSys == "Fire")
			{
				return list3;
			}
			return null;
		}

		private List<MEPSystem> mepSystems;

		private Document doc;

		private UIDocument uiDoc;

		private string commandSysName = "";

		private string showImage = Path.Combine(Product.ResLocation, "systemShow.png");

		private string hideImage = Path.Combine(Product.ResLocation, "systemHide.png");

		private string brushImage = Path.Combine(Product.ResLocation, "systemBrush.png");

		private string locateIamge = Path.Combine(Product.ResLocation, "location.png");

		public string specialSysName = "";

		private string projectName = "";

		private Button tatelButton;
	}
}
